package deploy

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/deploy"
	deployReq "github.com/flipped-aurora/gin-vue-admin/server/model/deploy/request"
)

type DeployAuthorizedService struct{}

// CreateDeployAuthorized 创建授权管理记录
// Author [piexlmax](https://github.com/piexlmax)
func (deployAuthorizedService *DeployAuthorizedService) CreateDeployAuthorized(deployAuthorized *deploy.DeployAuthorized) (err error) {
	err = global.GVA_DB.Create(deployAuthorized).Error
	return err
}

// DeleteDeployAuthorized 删除授权管理记录
// Author [piexlmax](https://github.com/piexlmax)
func (deployAuthorizedService *DeployAuthorizedService) DeleteDeployAuthorized(id string) (err error) {
	err = global.GVA_DB.Delete(&deploy.DeployAuthorized{}, "id = ?", id).Error
	return err
}

// DeleteDeployAuthorizedByIds 批量删除授权管理记录
// Author [piexlmax](https://github.com/piexlmax)
func (deployAuthorizedService *DeployAuthorizedService) DeleteDeployAuthorizedByIds(ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]deploy.DeployAuthorized{}, "id in ?", ids).Error
	return err
}

// UpdateDeployAuthorized 更新授权管理记录
// Author [piexlmax](https://github.com/piexlmax)
func (deployAuthorizedService *DeployAuthorizedService) UpdateDeployAuthorized(deployAuthorized deploy.DeployAuthorized) (err error) {
	err = global.GVA_DB.Model(&deploy.DeployAuthorized{}).Where("id = ?", deployAuthorized.Id).Updates(&deployAuthorized).Error
	return err
}

// GetDeployAuthorized 根据id获取授权管理记录
// Author [piexlmax](https://github.com/piexlmax)
func (deployAuthorizedService *DeployAuthorizedService) GetDeployAuthorized(id string) (deployAuthorized deploy.DeployAuthorized, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&deployAuthorized).Error
	return
}

// GetDeployAuthorizedInfoList 分页获取授权管理记录
// Author [piexlmax](https://github.com/piexlmax)
func (deployAuthorizedService *DeployAuthorizedService) GetDeployAuthorizedInfoList(info deployReq.DeployAuthorizedSearch) (list []deploy.DeployAuthorized, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&deploy.DeployAuthorized{})
	var deployAuthorizeds []deploy.DeployAuthorized
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.App != "" {
		db = db.Where("app LIKE ?", "%"+info.App+"%")
	}
	if info.StartEndtime != nil && info.EndEndtime != nil {
		db = db.Where("endtime BETWEEN ? AND ? ", info.StartEndtime, info.EndEndtime)
	}
	if info.Switch != nil {
		db = db.Where("switch = ?", info.Switch)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["app"] = true
	orderMap["id"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&deployAuthorizeds).Error
	return deployAuthorizeds, total, err
}
